/**   
 * @ClassName: MenuController.java 
 * @Package com.vrv.controller.menu 
 * @author LQ   
 * @date 2017年4月8日 上午5:30:25 
 * @version V1.0
 */
package com.vrv.controller.menu;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.vrv.button.model.Button;
import com.vrv.button.service.ButtonService;
import com.vrv.common.annotation.SysLog;
import com.vrv.common.constants.Constants;
import com.vrv.common.util.page.PageUtils;
import com.vrv.common.util.result.ResultMapper;
import com.vrv.controller.base.AbstractController;
import com.vrv.menu.model.Menu;
import com.vrv.menu.service.MenuService;
import com.vrv.menubutton.model.SaveMenuButton;
import com.vrv.menubutton.service.MenuButtonService;




/** 
 * MenuController:菜单Controller
 * @author LQ 
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends AbstractController{
    
    /**
     * 菜单service
     */
    @Autowired
    private MenuService menuService;
    
    /**
     * 按钮service
     */
    @Autowired
    private ButtonService buttonService;
    
    /**
     * 菜单按钮service
     */
    @Autowired
    private MenuButtonService menuButtonService;
    
    /**
     * 菜单列表
     * @param page 页
     * @param limit 行
     * @return ResultMapper 结果集
     */
    @RequestMapping("/list")
    public ResultMapper list(Integer page, Integer limit){
        List<Menu> list = menuService.list(page, limit);
        PageInfo<Menu> pageInfo = new PageInfo<Menu>(list);
        PageUtils pageUtil = new PageUtils(list, pageInfo.getTotal(), limit, page);
        return ResultMapper.ok().put("page", pageUtil);
    }
    
    /**
     * 获取用户菜单
     * @return ResultMapper 结果集
     */
    @RequestMapping("/queryUserMenuList")
    public ResultMapper queryUserMenuList(){
        List<Menu> menuList = menuService.queryUserMenuList(getUserCode());
        return ResultMapper.ok().put("menuList", menuList);
    }
    
    /**
     * 获取下拉菜单树
     * @return ResultMapper 结果集
     */
    @RequestMapping("/selectMenuTree")
    public ResultMapper selectMenuTree(){
        List<Menu> menuList = menuService.list();
        //添加顶级菜单
        Menu root = new Menu();
        root.setMenuCode(0);
        root.setMenuName("一级菜单");
        root.setParentCode(-1);
        root.setOpen(true);
        menuList.add(root);
        return ResultMapper.ok().put("menuList", menuList);
    }
    
    /**
     * 新增菜单
     * @param menu 菜单实体
     * @return ResultMapper 结果集
     */
    @SysLog("新增菜单")
    @RequestMapping("/add")
	public ResultMapper save(@RequestBody Menu menu){
        //数据校验
        //verifyForm(menu);
        menuService.add(menu);
        return ResultMapper.ok();
    }
    
    /**
     * 菜单信息
     * @param menuCode 菜单编号
     * @return ResultMapper 结果集
     */
    @RequestMapping("/info/{menuCode}")
    public ResultMapper info(@PathVariable("menuCode") Integer menuCode){
        Menu menu = menuService.getMenuByCode(menuCode);
        return ResultMapper.ok().put("menu", menu);
    }
    
    /**
     * 修改菜单
     * @param menu 菜单实体
     * @return ResultMapper 结果集
     */
    @SysLog("修改菜单")
    @RequestMapping("/update")
    public ResultMapper update(@RequestBody Menu menu){
        menuService.update(menu);
        return ResultMapper.ok();
    }
    
    /**
     * 删除菜单
     * @param menuCodes 菜单数组
     * @return ResultMapper 结果集
     */
    @SysLog("删除菜单")
    @RequestMapping("/delete")
    public ResultMapper delete(@RequestBody Integer[] menuCodes){
        ResultMapper r = menuService.deleteBatch(menuCodes);
        return r;
    }
    
    /**
     * 根据菜单编号获取所有的按钮和已添加的按钮
     * @param menuCode 菜单编号
     * @return ResultMapper 结果集
     */
    @RequestMapping("/selectMenuButton/{menuCode}")
    public ResultMapper selectMenuButton(@PathVariable("menuCode") Integer menuCode){
        ResultMapper r = new ResultMapper();
        List<Button> buttonList = buttonService.list();
        Integer[] menuButtonList = menuButtonService.queryMenuButtonByMenuCode(menuCode);
        r.put("buttonList", buttonList);
        r.put("menuButtonList", menuButtonList);
        return r;
    }
    
    /**
     * 设置菜单按钮
     * @param menuCode 菜单数组
     * @param menuButtonList 按钮数组
     * @return ResultMapper 结果集
     */
    @SysLog("设置菜单按钮")
    @RequestMapping("/saveButton")
    public ResultMapper saveButton(@RequestBody SaveMenuButton menuButtonList){
        menuButtonService.saveMenuButton(menuButtonList);
        return ResultMapper.ok(Constants.RESULT_MESSAGE_SAVE_SUCCESS);
    }
    
}
